<?php

class EmploymentContractTypeForm extends BaseForm
{

    private $employmentContractTypeService;
    public $employmentContractTypeId;

    public function getEmploymentContractTypeService()
    {
        if (is_null($this->employmentContractTypeService)) {
            $this->employmentContractTypeService = new EmploymentContractTypeService();
            $this->employmentContractTypeService->setEmploymentContractTypeDao(new EmploymentContractTypeDao());
        }
        return $this->employmentContractTypeService;
    }

    public function configure()
    {

        $this->employmentContractTypeId = $this->getOption('employmentContractTypeId');

        $this->setWidgets(array(
            'employmentContractTypeCode' => new sfWidgetFormInputText(),
            'employmentContractTypeName' => new sfWidgetFormInputText(),
            'employmentContractTypeDescription' => new sfWidgetFormTextArea()
        ));

        $this->setValidators(array(
            'employmentContractTypeCode' => new sfValidatorString(array('required' => true, 'max_length' => 10, 'trim' => true)),
            'employmentContractTypeName' => new sfValidatorString(array('required' => true, 'max_length' => 100, 'trim' => true)),
            'employmentContractTypeDescription' => new sfValidatorString(array('required' => false, 'max_length' => 1000, 'trim' => true))
        ));

        $this->widgetSchema->setNameFormat('employmentContractType[%s]');

        if (!empty($this->employmentContractTypeId)) {
            $employmentContractType = $this->getEmploymentContractTypeService()->getEmploymentContractTypeById($this->employmentContractTypeId);

            $this->setDefault('employmentContractTypeCode', $employmentContractType->getCode());
            $this->setDefault('employmentContractTypeName', $employmentContractType->getName());
            $this->setDefault('employmentContractTypeDescription', $employmentContractType->getDescription());
        }
    }

    public function save()
    {
        $resultArray = array();

        $employmentContractTypeCode = $this->getValue('employmentContractTypeCode');
        $employmentContractTypeName = $this->getValue('employmentContractTypeName');
        $employmentContractTypeDescription = $this->getValue('employmentContractTypeDescription');

        if (!empty($this->employmentContractTypeId)) {
            $employmentContractTypeObj = $this->getEmploymentContractTypeService()->getEmploymentContractTypeById($this->employmentContractTypeId);
            $resultArray['messageType'] = 'success';
            $resultArray['message'] = __(TopLevelMessages::UPDATE_SUCCESS);
        } else {
            $employmentContractTypeObj = new EmploymentContractType();
            $resultArray['messageType'] = 'success';
            $resultArray['message'] = __(TopLevelMessages::SAVE_SUCCESS);
        }

        $employmentContractTypeObj->setCode($employmentContractTypeCode);
        $employmentContractTypeObj->setName($employmentContractTypeName);
        $employmentContractTypeObj->setDescription($employmentContractTypeDescription);

        $employmentContractTypeObj->save();

        return $resultArray;
    }

    public function getEmploymentContractTypeListAsJson()
    {

        $list = array();
        $employmentContractTypeList = $this->getEmploymentContractTypeService()->getEmploymentContractTypeList();
        foreach ($employmentContractTypeList as $employmentContractType) {
            $list[] = array('id' => $employmentContractType->getId(), 'code' => $employmentContractType->getCode());
        }
        return json_encode($list);
    }

}

